package com.atguigu0.core

import org.apache.spark.broadcast.Broadcast
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @description: 广播变量,将大的变量发布到executor中
 * @time: 2020/6/15 11:10
 * @author: baojinlong
 **/
object BoardcastValueTest {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setAppName("myWordCount").setMaster("local[*]")
    // 创建sparkContext对象,转换操作是懒执行的,并不会马上触发job
    val sc: SparkContext = new SparkContext(sparkConf)
    // 定义一个广播变量
    val castValue: Broadcast[Array[Int]] = sc.broadcast(Array(1, 2, 3))
    val numRDD: RDD[Int] = sc.parallelize(Array(1, 2, 3, 4), 2)
    val transformData: RDD[(Int, Int)] = numRDD.map(x => {
      (x, castValue.value(1))
    })
    transformData.foreach(println)
    // 关闭连接
    sc.stop()
  }
}
